package com.shop.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.Service.ClassifyService;
import com.shop.pojo.Classifies;
import com.shop.pojo.Classify;
import com.shop.pojo.Message;
import com.shop.mapper.ClassifyMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {
    @Override
    public Message<String> addClassify(Classify classify) throws IOException {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        int i = mapper.save(classify);
        if (i>0){
            sqlSession.commit();
            message.setMessage("添加成功");
            message.setData("添加成功");
        }else {
            message.setMessage("添加失败");
        }
        return message;
    }

    @Override
    public Message<PageInfo<Classify>> selectAllClassify(String queryInfo, int pageSize, int pageNum) throws IOException {
        Message<PageInfo<Classify>> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<Classify> classifyList =  mapper.selectClassify(queryInfo);
        message.setMessage("获取成功");
        PageInfo<Classify> pageInfo = new PageInfo<Classify>(classifyList);
        message.setData(pageInfo);
        return message;
    }
    @Override
    public Message<String> deleteClassify(int id) throws IOException {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        int i = mapper.deleteClassify(id);
        if (i>0){
            sqlSession.commit();
            message.setMessage("删除成功");
            message.setData("删除成功");
        }else {
            message.setMessage("删除失败");
        }
        return message;
    }
    @Override
    public Message<String> updateClassify(Classify classify) throws IOException{
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        int i = mapper.updateClassify(classify);
        if (i>0){
            sqlSession.commit();
            message.setMessage("修改成功");
            message.setData("修改成功");
        }else {
            message.setMessage("修改失败");
        }
        return message;
    }

    @Override
    public Message<List<Classify>> selectAll() throws IOException {
        Message<List<Classify>> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        List<Classify> classifyList = mapper.selectAll();
        message.setMessage("获取成功");
        message.setData(classifyList);
        return message;
    }

    @Override
    public Message<PageInfo<Classifies>> selectNew(String queryInfo, int pageSize, int pageNum) throws IOException {
        Message<PageInfo<Classifies>> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<Classifies> classifyList =  mapper.selectNew(queryInfo);
        message.setMessage("获取成功");
        PageInfo<Classifies> pageInfo = new PageInfo<Classifies>(classifyList);
        message.setData(pageInfo);
        return message;
    }

    @Override
    public Message<Classify> selectById(int fid) throws Exception {
        Message<Classify> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassifyMapper mapper = sqlSession.getMapper(ClassifyMapper.class);
        Classify classify = new Classify();
        classify = mapper.selectById(fid);
        message.setData(classify);
        message.setMessage("获取成功");
        return message;
    }

}
